package com.zyd.blog.personal.service.impl;

import	java.util.HashMap;
import	java.util.Map;
import	java.util.ArrayList;

import com.zyd.blog.personal.enuminterface.IsDelete;
import com.zyd.blog.personal.enuminterface.Step;
import com.zyd.blog.personal.service.DesignerApplyDealService;
import com.zyd.blog.personal.service.UserApplyDesignTempService;
import com.zyd.blog.personal.utils.DateUtils;
import com.zyd.blog.personal.utils.JavaBeanAndMapUtils;
import com.zyd.blog.personal.utils.StringUtils;
import com.zyd.blog.personal.vo.DesignerApplyInfoConditionVo;
import com.zyd.blog.personal.vo.ReviewResultVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *  设计师申请处理业务逻辑层实现
 * @author mfy
 */
@Service
public class DesignerApplyDealServiceImpl implements DesignerApplyDealService {

    @Resource
    private UserApplyDesignTempService userApplyDesignTempService;
    private List<Map<String,Object>> getDesignerApplyInfoBycondition(DesignerApplyInfoConditionVo designerApplyInfoConditionVo){
        Map<String,Object> condition;
        condition = JavaBeanAndMapUtils.javaBeanToMap(designerApplyInfoConditionVo);
        condition.put("applyTimeStart", DateUtils.parseStringToLong((String) condition.get("applyTimeStart")));
        condition.put("applyTimeEnd", DateUtils.parseStringToLong((String) condition.get("applyTimeEnd")));
        condition.put("isSubmit", "Y");
        condition.put("isDelete", IsDelete.IS_DELETE_NO);
        return userApplyDesignTempService.getAllUserApplyDesignInfo(condition);
    }

    /**
     * 转化step的属性值
     * @param step  判断值
     * @return  结果值
     */
    private String judgeStep(String step){
        switch (step){
            case "2":
                return "待审核";
            case "3":
                return "审核通过";
            case "4":
                return "审核不通过";
            default :
                return "未提交申请";
        }
    }


    private  List<Map<String,Object>> formatDesignerApplyInfo(List<Map<String,Object>> userApplyDesignInfos){
        if(userApplyDesignInfos == null || userApplyDesignInfos.size() == 0){
            return null;
        }
        List<Map<String,Object>> result = new ArrayList<> ();
        for (Map<String,Object> item : userApplyDesignInfos){
            String step = (String) item.get("step");
            if("3".equals(step)){
                continue;
            }
            Map<String,Object> userApplyDesignInfo = new HashMap<> (16);
            userApplyDesignInfo.put("id",item.get("id"));
            userApplyDesignInfo.put("userId",item.get("userId"));
            userApplyDesignInfo.put("sex",item.get("sex"));
            userApplyDesignInfo.put("username",item.get("username"));
            userApplyDesignInfo.put("companyName",item.get("companyName"));
            userApplyDesignInfo.put("workAddress", StringUtils.getAddressOnlyProvinceAndCity((String) item.get("workAddress")));
            userApplyDesignInfo.put("step",item.get("step"));
            userApplyDesignInfo.put("reason",judgeStep(step));
            userApplyDesignInfo.put("updateTime", DateUtils.parseLongToString((Long) item.get("updateTime"),"yyyy-MM-dd HH:mm:ss"));
            result.add(userApplyDesignInfo);
        }
        return  result;
    }


    @Override
    public List<Map<String,Object>> getDesignerApplyInfo(DesignerApplyInfoConditionVo designerApplyInfoConditionVo){
        List<Map<String,Object>> userApplyDesignInfos = getDesignerApplyInfoBycondition(designerApplyInfoConditionVo);
        if(userApplyDesignInfos == null || userApplyDesignInfos.size() == 0){
            return null;
        }
        return formatDesignerApplyInfo(userApplyDesignInfos);
    }

    /**
     * 格式化一些属性
     * @param condition 需要格式化的对象
     * @return  格式化后的对象
     */
    private Map<String,Object> formatDesignerApplyDetailInfo(Map<String,Object> condition){
        condition.remove("isSubmit");
        condition.remove("isDelete");
        condition.remove("updateTime");
        condition.remove("addTime");
        condition.put("submitTime",DateUtils.parseLongToString((Long) condition.get("submitTime"),"yyyy-MM-dd HH:mm"));
        condition.put("birthday",DateUtils.parseLongToString((Long) condition.get("birthday"),"yyyy.MM.dd"));
        condition.put("designTime",DateUtils.parseLongToString((Long) condition.get("designTime"),"yyyy"));
        condition.put("liveAddress",StringUtils.getAddressOfStr((String) condition.get("liveAddress")));
        condition.put("workAddress",StringUtils.getAddressOfStr((String) condition.get("workAddress")));
        condition.put("serviceAddress",StringUtils.getStringOfSplit((String) condition.get("serviceAddress")));
        condition.put("businessLicense",StringUtils.getStringOfSplit((String) condition.get("businessLicense")));
        condition.put("designType",StringUtils.parsingJsonArray((String) condition.get("designType"),"content"));
        String step = (String)condition.get("step");
        switch (step){
            case Step.STEP_REVIEW_YES:
                condition.put("result","审核通过");
                break;
            case Step.STEP_REVIEW_NO:
                condition.put("result","审核不通过");
                break;
            case Step.STEP_SUBMIT_NOREVIEW:
                condition.put("result","待审核");
                break;
                default :
                    break;
        }
        Long reviewTime = (Long) condition.get("reviewTime");
        if(reviewTime != null){
            condition.put("reviewTime",DateUtils.parseLongToString(reviewTime,"yyyy-MM-dd HH:mm"));
        }
        return condition;
    }

    @Override
    public Map<String,Object> getDesignerApplyDetailInfo(DesignerApplyInfoConditionVo designerApplyInfoConditionVo){
        String id = designerApplyInfoConditionVo.getId();
        if(id == null || "".equals(id.trim())){
            return null;
        }
        List<Map<String,Object>> userApplyDesignInfos = getDesignerApplyInfoBycondition(designerApplyInfoConditionVo);
        if(userApplyDesignInfos == null || userApplyDesignInfos.size() == 0){
            return null;
        }else {
            return formatDesignerApplyDetailInfo(userApplyDesignInfos.get(0));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean review(ReviewResultVo review){
        String step = review.getStep();
        switch (step){
            case "3":
                return userApplyDesignTempService.reViewApplyDesign(Long.parseLong(review.getId()),Long.parseLong(review.getUserId()),1,review.getReason(),review.getReviewer());
            case "4":
                return userApplyDesignTempService.reViewApplyDesign(Long.parseLong(review.getId()),Long.parseLong(review.getUserId()),2,review.getReason(),review.getReviewer());
                default :break;
        }
        return false;
    }

    @Override
    public boolean delete(Long id){
        if(id == null){
            return false;
        }
        return userApplyDesignTempService.deleteReconding(id);
    }

}
